<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Robot
 *
 * @author Implement <hdjahs@qq.com>
 */
class Robot {

    private $strategy = array();
    private $ids = array();
    private $updateSql = '';
    
    public function setStrategy($obj) {
        $this->strategy[] = $obj;
    }

    public function run() {
        foreach ($this->strategy as $obj) {
            $objName = get_class($obj);
            $keys = $this->getKeys($objName);
            $obj->init($keys, $this->ids);
            $this->update();
        }
    }

    public function getKeys($objName) {
        switch ($objName) {
            case 'Baidu':
                $sql = "SELECT `articleid`,`articlename`,`author` FROM `jieqi_article_article` WHERE finish_author <> 1 LIMIT 0,5";
                $this->updateSql = "UPDATE `jieqi_article_article` SET finish_author = 1 WHERE articleid IN(%s)";
                $type = "author";
                break;
            case 'Qidian':
                $sql = "SELECT `articleid`,`articlename`,`author` FROM `jieqi_article_article` WHERE finish_comment <> 1 LIMIT 0,5";
                $this->updateSql = "UPDATE `jieqi_article_article` SET finish_comment = 1 WHERE articleid IN(%s)";
                $type = "articlename";
                break;
            case 'Zongheng':
                break;
            default:
                break;
        }

        $db = mysql::getIns();
        $result = $db->select($sql);
        $this->ids = array_column($result, 'articleid');

        return array_column($result, $type);
    }

    public function update() {
        $ids = implode(',', $this->ids);
        if (!empty($ids)) {
            $sql = sprintf($this->updateSql, $ids);
            $db = mysql::getIns();
            $db->update($sql);
        }
    }

}
